/****************************************************************/
/*  I M P E T U S     C O P Y R I G H T      S T A T E M E N T  */
/****************************************************************/
/*                                                              */
/*  NOTICE: THIS SOFTWARE CONTAINS TRADE SECRETS THAT BELONG TO */
/*          IMPETUS INFOTECH, AND IS LICENSED BY AN AGREEMENT.  */
/*          ANY UNAUTHORIZED ACCESS, USE, DUPLICATION OR        */
/*          DISCLOSURE IS UNLAWFUL.                             */
/*                                                              */
/*          COPYRIGHT (C) 2008 IMPETUS INFOTECH INDIA PVT LTD   */
/*          ALL RIGHTS RESERVED.                                */
/****************************************************************/

package com.iLabs.faces.custom.validators;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

public abstract class ValidatorMessageUtils {
	
	private static final String DEFAULT_MESSAGE="default_message";
	
	private static final String VALIDATION_MESSAGE_BUNDLE="com.iLabs.faces.validation.messages.nl.CustomValidationMessages";
	
	/**
	 * This method is used to get the validator message from the resource bundle
	 * 
	 * @return String validator message
	 */
	static void logValidationMessage(FacesContext context, UIComponent component,
			Object value,String validatorMessageId) throws ValidatorException {
		FacesMessage message = new FacesMessage();
		String validationMessage = null;
		ResourceBundle bundle = ResourceBundle.getBundle(VALIDATION_MESSAGE_BUNDLE, context.getViewRoot()
				.getLocale());
		try {			
			validationMessage = bundle.getString(validatorMessageId);								
		} catch(MissingResourceException missingResourceException){
			validationMessage = bundle.getString(DEFAULT_MESSAGE);
		}		
		String formattedMessage=MessageFormat.format(validationMessage, new Object[]{component.getId()});
		message.setDetail(formattedMessage);
		message.setSummary(formattedMessage);
		message.setSeverity(FacesMessage.SEVERITY_ERROR);
		throw new ValidatorException(message);
	}
}
